/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: DrivingLights.java Package:
 * tc.de.krawallmann.robobridge.devices.implementation Version: 1.0 Author(s):
 * KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.devices.implementation;

import java.util.BitSet;

import tc.de.krawallmann.robobridge.communication.Command;
import tc.de.krawallmann.robobridge.communication.CommandType;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.Robot;

/**
 * The Class DrivingLights interfaces my custom driving lights.<br/>
 * It's probably not useful for other users.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class DrivingLights extends AbstractDevice {

	public static final String BLINKING_RIGHT = "BLINKING_RIGHT";

	public static final String BLINKING_LEFT = "BLINKING_LEFT";

	public static final String HEADLIGHTS = "HEADLIGHTS";

	public static final String REVERSELIGHT = "REVERSELIGHT";

	public static final String WARN_BLINKING = "WARN_BLINKING";

	/** The blinking left. */
	private boolean blinkingLeft;

	/** The blinking right. */
	private boolean blinkingRight;
	private BitSet leftSet;
	private BitSet rightSet;

	/** The headlights on. */
	private boolean headlightsOn;

	/** The reverse light on. */
	private boolean reverseLightOn;

	/** The warn blinking. */
	private boolean warnBlinking = true;
	

	/**
	 * Instantiates a new driving lights.
	 * 
	 * @param robot
	 *            the robot
	 * @param deviceId
	 *            the device id
	 */
	public DrivingLights(final Robot robot, final int deviceId) {
		super(robot, AbstractDevice.DEVICE_ID_RP6_DRIVING_LIGHTS, deviceId, "KrawallMann", "DrivingLights");
		leftSet = new BitSet(8);
		rightSet = new BitSet(8);
	}

	/**
	 * Checks if is blinking left.
	 * 
	 * @return true, if is blinking left
	 */
	public boolean isBlinkingLeft() {
		return blinkingLeft;
	}

	/**
	 * Checks if is blinking right.
	 * 
	 * @return true, if is blinking right
	 */
	public boolean isBlinkingRight() {
		return blinkingRight;
	}

	/**
	 * Checks if is warn blinking.
	 * 
	 * @return true, if is warn blinking
	 */
	public boolean isWarnBlinking() {
		return warnBlinking;
	}

	
	
	/*
	 * (non-Javadoc)
	 * @see
	 * basepackage.IncomingCommandListener#onCommandReceived(basepackage.Command
	 * )
	 */
	@Override
	public boolean onCommandReceived(final Command command) {
		return false;
	}


	/**
	 * Sets the blinking left.
	 * 
	 * @param blinkingLeft
	 *            the new blinking left
	 */
	public void setBlinkingLeft(final boolean blinkingLeft) {
		boolean old = this.blinkingLeft;
		this.blinkingLeft = blinkingLeft;
		if (blinkingLeft) {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 1));
			
		} else {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 4));
		}
		
		firePropertyChange(BLINKING_LEFT, old, blinkingLeft);
	}

	/**
	 * Sets the blinking right.
	 * 
	 * @param blinkingRight
	 *            the new blinking right
	 */
	public void setBlinkingRight(final boolean blinkingRight) {
		boolean old = this.blinkingRight;
		this.blinkingRight = blinkingRight;
		if (blinkingRight) {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 2));
		} else {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 4));
		}
		firePropertyChange(BLINKING_RIGHT, old, blinkingRight);
	}

	/**
	 * Sets the headlights on.
	 * 
	 * @param headlightsOn
	 *            the new headlights on
	 */
	public void setHeadlightsOn(final boolean headlightsOn) {
		boolean old = this.headlightsOn;
		this.headlightsOn = headlightsOn;
		if (headlightsOn) {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 2, 1));
		} else {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 2, 4));
		}
		firePropertyChange(HEADLIGHTS, old, headlightsOn);
	}

	/**
	 * Sets the reverse light on.
	 * 
	 * @param reverseLightOn
	 *            the new reverse light on
	 */
	public void setReverseLightOn(final boolean reverseLightOn) {
		boolean old = this.reverseLightOn;
		this.reverseLightOn = reverseLightOn;
		if (reverseLightOn) {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 3, 1));
		} else {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 3, 4));
		}
		firePropertyChange(REVERSELIGHT, old, reverseLightOn);
	}

	/**
	 * Sets the warn blinking.
	 * 
	 * @param warnBlinking
	 *            the new warn blinking
	 */
	public void setWarnBlinking(final boolean warnBlinking) {
		boolean old = this.warnBlinking;
		this.warnBlinking = warnBlinking;
		if (warnBlinking) {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 3));
		} else {
			robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false,
					getDeviceId(), 1, 4));
		}
		firePropertyChange(WARN_BLINKING, old, warnBlinking);
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.AbstractDevice#toString()
	 */
	@Override
	public String toString() {
		return "DrivingLights [deviceId=" + deviceId + ", manufacturer=" + manufacturer
				+ ", model=" + model + ", blinkingLeft=" + blinkingLeft + ", blinkingRight="
				+ blinkingRight + ", headlightsOn=" + headlightsOn + ", reverseLightOn="
				+ reverseLightOn + ", warnBlinking=" + warnBlinking + "]";
	}

	public boolean isHeadlightsOn() {
		return headlightsOn;
	}

	public boolean isReverseLightOn() {
		return reverseLightOn;
	}
}
